namespace Xmef.Serializer.Internal
{
    using System;
    using System.Collections;
    using Xmef.Interfaces;

    [Serializable]
    internal class XmlFormatterCache
    {
        private readonly Hashtable hashtable = new Hashtable();

        public IXmlTypeFormatter this[IXmlTypeFormatter formatter]
        {
            get
            {
                IXmlRuntimeSerializerInternal serializer = Runtime.GetSerializer(formatter);
                if (serializer != null)
                {
                    Type type = serializer.RuntimeType;
                    if (!this.hashtable.ContainsKey(type))
                    {
                        this.hashtable[type] = formatter;
                    }
                    return (IXmlTypeFormatter) this.hashtable[type];
                }
                return null;
            }
        }

        public IXmlTypeFormatter this[Type type]
        {
            get
            {
                IXmlTypeFormatter formatter = null;
                if (!this.hashtable.ContainsKey(type))
                {
                    formatter = new XmlTypeSerializer(type);
                    this.hashtable[type] = formatter;
                    return formatter;
                }
                return (IXmlTypeFormatter) this.hashtable[type];
            }
        }
    }
}

